package com.kaifamiao.tags;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class TableTag extends SimpleTagSupport {

    private Integer rows ;
    private Integer columns ;

    @Override
    public void doTag() throws JspException, IOException {
        if( rows == null || columns == null ){
            throw new RuntimeException( "未指定行数或列数" );
        }

        if( rows <= 0 || columns <= 0 ){
            throw new RuntimeException( "行数或列数必须大于零" );
        }

        JspContext ctx = this.getJspContext();
        JspWriter out = ctx.getOut();

        out.println( "<table border='1' align='center' width='80%'>" );

        for (int i = 1; i <= rows ; i++) {
            out.println( "<tr>");
            for (int j = 1; j <= columns; j++) {
                out.println( "<td> (" + i + "," + j + ") </td>" );
            }
            out.println( "</tr>");
        }

        out.println( "</table>" );

    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public Integer getColumns() {
        return columns;
    }

    public void setColumns(Integer columns) {
        this.columns = columns;
    }
}
